package com.xxkj.drugtraceability.ui.departhd

import android.text.Editable
import android.text.Html
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.hjq.shape.layout.ShapeLinearLayout
import com.xxkj.drugtraceability.R
import com.xxkj.drugtraceability.bean.DepartDrugInfo
import com.xxkj.drugtraceability.bean.DrugInfoBean
import com.xxkj.drugtraceability.databinding.LayoutDeparthddrugcollectionBinding
import com.xxkj.drugtraceability.databinding.LayoutDrugScan1Binding
import com.xxkj.drugtraceability.databinding.LayoutPrescriptiondrugcollectionBinding
import com.xxkj.drugtraceability.ext.clickN
import com.xxkj.drugtraceability.ext.showKeyboardIfNeeded
import com.xxkj.drugtraceability.ext.visible
import com.xxkj.drugtraceability.utils.CommonUtils

/**
 * author: zhoujingjin
 * created on: 2024/9/10 16:52
 * description:
 */
class DepartDrugHdCollectionAdapter(private val type:Int,
    private val skipClick: (index: Int) -> Unit,private val hdAction: (item: DepartDrugInfo) -> Unit) : BaseQuickAdapter<DepartDrugInfo, BaseDataBindingHolder<LayoutDeparthddrugcollectionBinding>>(R.layout.layout_departhddrugcollection) {
    private var scan = false
    fun setCanScan(scan: Boolean) {
        this.scan = scan
    }

    override fun convert(
        holder: BaseDataBindingHolder<LayoutDeparthddrugcollectionBinding>,
        item: DepartDrugInfo
    ) {
        holder.dataBinding?.apply {
            val buffer = StringBuffer()
            buffer.append("<font color='#33333F' size='50px'>")
            buffer.append("<b>" + item.mc + "    " + "</b>")
            buffer.append("</font>")
            buffer.append("<font color='#494953' size='24px'>")
            buffer.append( item.gg + "/" + item.dwmc)
            buffer.append("</font>")
            if(etNum.tag!=null){
                val tag =  etNum.tag as TextWatcher
                etNum.removeTextChangedListener(tag)
            }
            tvContent.text = Html.fromHtml(buffer.toString())
            if (item.isCheck=="1")
            etNum.setText(item.checkNum)
            else  etNum.setText(item.hdNum)

            tvUnit.text = item.dwmc
            etNum.onFocusChangeListener = null
            if (type==0){
                stvSkip.visibility = View.VISIBLE
                etNum.setOnFocusChangeListener { _, hasFocus ->
                    if (hasFocus){
                        //弹出键盘
                        etNum.showKeyboardIfNeeded()
                    }
                    if (etNum.text.toString().trim().isNotEmpty()&&!hasFocus){
                        //进入到已核对页面
                        item.isSync = true
                        item.hdNum = etNum.text.toString().trim()
                        etNum.onFocusChangeListener = null
                        hdAction.invoke(item)
                    }
                }
                stvSkip.clickN {
                    skipClick.invoke(getItemPosition(item))
                }
            }else{
                stvSkip.visibility = View.INVISIBLE
            }
            if (item.isCheck!="1"){
                etNum.isEnabled = true
                val textWatcher = object :TextWatcher{
                    override fun afterTextChanged(s: Editable?) {
                        item.hdNum = s.toString().trim()
                        item.isSync = !s.toString().trim().isEmpty()
                    }

                    override fun beforeTextChanged(
                        s: CharSequence?,
                        start: Int,
                        count: Int,
                        after: Int
                    ) {
                    }

                    override fun onTextChanged(
                        s: CharSequence?,
                        start: Int,
                        before: Int,
                        count: Int
                    ) {
                    }
                }
                etNum.addTextChangedListener(textWatcher)
                etNum.tag = textWatcher
            }else{
                etNum.isEnabled = false
            }
            //被选中
            if (type==0){
                llParent.isSelected = item.isSelected
                llParent.setOnClickListener {

                    data.forEach {
                        if (it!=item)
                        it.isSelected = false
                    }
                    item.isSelected = !item.isSelected
//                    if (item.isSelected) {
//                        actionClick.invoke(getItemPosition(item))
//                    } else {
//                        actionClick.invoke(-1)
//                    }
                    notifyDataSetChanged()
                }
            }
        }

    }
}